package cn.tedu;

import com.sun.javaws.IconUtil;
import sun.awt.windows.ThemeReader;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*public class Demo {
    private int size = 5;//生产与消费的临界值
    private Queue<Integer> q = new LinkedList<>();
    public static void main(String[] args) throws InterruptedException {
        Demo d = new Demo();
        new Thread(() -> {
                    d.producer();//执行生产者的run方法
            }, "A").start();
        new Thread(() -> {
                d.consumer();//执行消费者的run方法
        }, "B").start();
    }
    public void producer(){
        while (true){
            synchronized (q) {
                if (q.size() >= size) {//队列数量大于临界值，表示队列满了，就不再生产
                    try {
                       q.wait();//当前线程进入等待状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                q.notify();
                q.add(1);
                System.out.println("生产一个,目前剩余" + q.size());
            }

            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void consumer(){
            while (true){
                synchronized (q){
                    if (q.size() == 0) {//和上面一样，队列数量为零就不能消费了
                        try {
                            q.wait();//当前线程进入等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    q.notify();
                    q.poll();
                    System.out.println("消费一个,目前剩余" + q.size());
                }

                try {
                    Thread.sleep(new Random().nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
}*/
/*public class Demo {

    private int size = 5;//生产与消费的临界值
    private Queue<Integer> q = new LinkedList<>();
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        Demo d = new Demo();
        new Thread(() -> {
            d.producer();//执行生产者的run方法
        }, "A").start();
        new Thread(() -> {
            d.consumer();//执行消费者的run方法
        }, "B").start();
    }

    public void producer(){
        while (true) {

            lock.lock();
            try {
                if (q.size() >= size) {//队列数量大于临界值，表示队列满了，就不再生产
                    try {
                        condition.await();//当前线程进入等待状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                condition.signal();
                q.add(1);
                System.out.println("生产一个,目前剩余" + q.size());
            } finally {
                lock.unlock();
            }
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void consumer(){
        while (true) {

            lock.lock();
            try {
                if (q.size() == 0) {//和上面一样，队列数量为零就不能消费了
                    try {
                        condition.await();//当前线程进入等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                condition.signal();
                q.poll();
                System.out.println("消费一个,目前剩余" + q.size());
            } finally {
                lock.unlock();//必须手动释放锁
            }
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}*/
public class Demo{
    private int size = 5;//生产与消费的临界值
    private Queue<Integer> q = new LinkedBlockingQueue<>(5);//链表有界阻塞队列
    public static void main(String[] args) throws InterruptedException {
        Demo d = new Demo();
        new Thread(() -> {
            d.producer();//执行生产者的run方法
        }, "A").start();
        new Thread(() -> {
            d.consumer();//执行消费者的run方法
        }, "B").start();
    }

    public void producer(){
        while (true) {
            if (q.size() >= size){
                q.poll();
                System.out.println("队列满，不能生产了，剩余" + q.size());
            }else {
                q.add(1);
                System.out.println("生产一个，剩余" + q.size());
            }
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void consumer(){
        while (true) {
            if (q.size() == 0){
                q.add(1);
                System.out.println("队列没有了，不能消费了，生产一个，剩余" + q.size());
            }else {
                q.poll();
                System.out.println("消费一个，剩余" + q.size());
            }
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}

